package com.leyou.item.service.impl;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.service.SpecGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class SpecGroupServiceImpl implements SpecGroupService {
    @Autowired
    private SpecGroupMapper specGroupMapper;
    @Autowired
    private SpecParamMapper specParamMapper;
    @Override
    public List<SpecGroupDTO> querySpecGroupById(Long id) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(id);
        List<SpecGroup> list =this.specGroupMapper.select(specGroup);
        if (CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.DATA_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list,SpecGroupDTO.class);
    }

    @Override
    public List<SpecParamDTO> querySpecParamByGid(Long gid,Long cid,Boolean searching) {
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);
      List<SpecParam> list  = this.specParamMapper.select(specParam);
      if (CollectionUtils.isEmpty(list)){
          throw new LyException(ExceptionEnum.DATA_NOT_FOUND);
      }
      return BeanHelper.copyWithCollection(list,SpecParamDTO.class);
    }

    @Override
    @Transactional
    public void addSpecGroup(SpecGroupDTO specGroupDTO) {
        SpecGroup specGroup = BeanHelper.copyProperties(specGroupDTO, SpecGroup.class);
        if (StringUtils.isEmpty(specGroup)){
            throw new LyException(ExceptionEnum.DATA_NOT_FOUND);
        }
        this.specGroupMapper.insert(specGroup);
    }

    @Override
    @Transactional
    public void editSpecGroup(SpecGroupDTO specGroupDTO) {
        SpecGroup specGroup = BeanHelper.copyProperties(specGroupDTO, SpecGroup.class);
        if (StringUtils.isEmpty(specGroup)){
            throw new LyException(ExceptionEnum.DATA_EDIT_FAIL);
        }
        this.specGroupMapper.updateByPrimaryKey(specGroup);
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        this.specGroupMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void addSpecParam(SpecParamDTO specParamDTO) {
        SpecParam specParam = BeanHelper.copyProperties(specParamDTO, SpecParam.class);
        this.specParamMapper.insertSelective(specParam);
    }

    @Override
    @Transactional
    public void editSpecParam(SpecParamDTO specParamDTO) {
        SpecParam specParam = BeanHelper.copyProperties(specParamDTO, SpecParam.class);
        if (StringUtils.isEmpty(specParam)){
            throw new  LyException(ExceptionEnum.DATA_EDIT_FAIL);
        }
        this.specParamMapper.updateByPrimaryKey(specParam);
    }

    @Override
    @Transactional
    public void deleteSpecParamById(Long id) {
        this.specParamMapper.deleteByPrimaryKey(id);
    }
}
